﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Code0914
{
    public class Touch_Control : MonoBehaviour
    {
        [Header("鼠标点击控制物体的Tag")]
        public string objectTag;

        [Header("控制方式切换：长按或点击")]
        public bool contrlState = false;

        [Header("点击后是否抬起")]
        public bool liftState = false;

        [Header("抬起高度")]
        public float liftHeight = 0.5f;

        #region 射线检测部分

        Ray ray; // 检测射线

        Transform hit_trans; // 射线检测物体的 Transform
        Vector3 hit_origin_position; // 射线检测物体的最初 Position
        Vector3 hit_mouse_position; // 物体跟随鼠标的 Position

        bool touch_state = false; // 点击状态

        #endregion

        // Update is called once per frame
        void Update()
        {
            // 控制方式选择
            if (contrlState) // 点击控制
            {

                if (Input.GetMouseButtonDown(0) && touch_state)
                {
                    touch_state = false;
                }
                else // touch_state 为否时才进行物体检测
                {
                    CastObject();
                }
            }
            else // 长按控制
            {
                if (!touch_state) // touch_state 为否时才进行物体检测
                {
                    CastObject();   
                }
                else
                {
                    if (Input.GetMouseButtonUp(0))
                    {
                        touch_state = false;
                    }
                }

            }

            // 判断点击移动
            if (touch_state)
            {
                hit_mouse_position = Camera.main.ScreenToWorldPoint(Input.mousePosition);

                // 判断是否抬起
                if(liftState)
                {
                    hit_trans.position = new Vector3(hit_mouse_position.x, hit_origin_position.y + liftHeight, hit_mouse_position.z);
                }
                else
                {
                    hit_trans.position = new Vector3(hit_mouse_position.x, hit_origin_position.y, hit_mouse_position.z);
                }

            }
            else
            {
                // 移动完成后，判断是否抬起是否复原
                if(liftState)
                {
                    if(hit_trans != null) // 空引用检查
                    {
                        if (hit_trans.position.y > hit_origin_position.y)
                        {
                            hit_trans.position = new Vector3(hit_trans.position.x, hit_origin_position.y, hit_trans.position.z);
                        }
                    }
                   
                }
            }
        }

        // 物体检测函数
        void CastObject()
        {
            // 设置射线
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            // 判断点击
            if (Input.GetMouseButtonDown(0))
            {
                RaycastHit hit;

                // 射线检测
                if (Physics.Raycast(ray, out hit))
                {

                    if (hit.transform.tag == objectTag)
                    {
                        touch_state = true;

                        // 设置检测物体
                        hit_trans = hit.transform;
                        hit_origin_position = hit_trans.position;
                    }
                }
            }
        }
    }
}
